package com.yq.controller.system;

import com.yq.config.shiro.ShiroSecurityUtils;
import com.yq.dto.DictTypeDto;
import com.yq.servicce.DictTypeService;
import com.yq.vo.AjaxResult;
import com.yq.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: yangQ
 * @Date: 2020/11/24 19:39
 * @Description:数据字典信息
 */
@RestController
@RequestMapping("system/dict/type")
public class DictTypeController {
    @Autowired
    private DictTypeService dictTypeService;

    /**
     * 分页查询字典类型数据
     *
     * @param dictTypeDto
     * @return
     */
    @GetMapping("listForPage")
    public AjaxResult listForPage(DictTypeDto dictTypeDto) {
        DataGridView dataGridView = dictTypeService.listPage(dictTypeDto);
        return AjaxResult.success("查询成功", dataGridView.getData(), dataGridView.getTotal());
    }

    /**
     * 查询所有的字典类型
     *
     * @return
     */
    @GetMapping("selectAllDictType")
    public AjaxResult selectAllDictType() {
        return AjaxResult.success("查询成功", this.dictTypeService.list());
    }

    /**
     * 添加一个字典类型数据
     *
     * @param dictTypeDto
     * @return
     */
    @PostMapping("addDictType")
    public AjaxResult addDictType(@Validated DictTypeDto dictTypeDto) {
        Boolean checkTrue = dictTypeService.checkDictTypeUnique(dictTypeDto.getDictId(), dictTypeDto.getDictType());
        if (checkTrue) {
            return AjaxResult.fail("新增字典【" + dictTypeDto.getDictName() + "】失败，字典类型已存在");
        }
        dictTypeDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.dictTypeService.insert(dictTypeDto));
    }

    /**
     * 更新字典数据类型
     *
     * @param dictTypeDto
     * @return
     */
    @PutMapping("updateDictType")
    public AjaxResult updateDictType(@Validated DictTypeDto dictTypeDto) {
        Boolean checkTrue = dictTypeService.checkDictTypeUnique(dictTypeDto.getDictId(), dictTypeDto.getDictType());
        if (checkTrue) {
            return AjaxResult.fail("修改失败,该字典类型已经存在");
        }
        dictTypeDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.dictTypeService.update(dictTypeDto));
    }

    /**
     * 批量删除字典类型
     * @param dictIds
     * @return
     */
    @DeleteMapping("deleteDictTypeByIds/{dictIds}")
    public AjaxResult deleteDictTypeByIds(@PathVariable Long[] dictIds) {
        return AjaxResult.toAjax(this.dictTypeService.deleteDictTypeByIds(dictIds));
    }

    /**
     * 根据ID查询一个字典类型
     * @param dictId
     * @return
     */
    @GetMapping("getOne/{dictId}")
    public AjaxResult getOne(@PathVariable Long dictId){
        return AjaxResult.success(this.dictTypeService.selectDictTypeById(dictId));
    }
}
